import { _decorator, Component, Label, Node } from 'cc';
import { GameManager, GameState } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('BgCtrl')
export class BgCtrl extends Component {

    @property(Node)
    stars: Node = null;

    @property(Node)
    farPlanets: Node = null;

    @property(Node)
    bigPlanet: Node = null;

    @property(Node)
    ringPlanets: Node = null;

    @property(Node)
    space_000: Node = null;

    @property(Node)
    space_001: Node = null;

    @property
    speed: number = 100;

    @property(Node)
    player: Node = null;

    @property(Label)
    goldCountLabel:Label=null;

    @property(Node)
    home: Node = null;      //home节点

    start() {
        
    }

    update(deltaTime: number) {

        if (GameManager.getInstance().gameState === GameState.GAME_OVER) {
            this.home.active = true;

        }else if(GameManager.getInstance().gameState === GameState.START){
            this.home.active = false;
            this.bgCtrl(deltaTime)
            this.goldCountLabel.string=GameManager.getInstance().getGoldCount().toString();

        }else{
            this.home.active = false;
            
        }
    }
    bgCtrl(deltaTime){

        const pos_1 = this.stars.position.clone();
        if (pos_1.y <= -1280) {
            pos_1.y = 1280;
        }
        this.stars.setPosition(pos_1.x, pos_1.y - (this.speed - 75) * deltaTime, pos_1.z);

        const pos_2 = this.farPlanets.position.clone();
        if (pos_2.y <= -1280) {
            pos_2.y = 1280;
        }
        this.farPlanets.setPosition(pos_2.x, pos_2.y - (this.speed - 65) * deltaTime, pos_2.z);

        const pos_3 = this.bigPlanet.position.clone();
        if (pos_3.y <= -1280) {
            pos_3.y = 1280;
        }
        this.bigPlanet.setPosition(pos_3.x, pos_3.y - (this.speed - 30) * deltaTime, pos_3.z);

        const pos_4 = this.ringPlanets.position.clone();
        if (pos_4.y <= -1280) {
            pos_4.y = 1280;
        }
        this.ringPlanets.setPosition(pos_4.x, pos_4.y - (this.speed - 40) * deltaTime, pos_4.z);

        const pos_5 = this.space_000.position.clone();
        if (pos_5.y < -1280) {
            pos_5.y = 1280;
        }
        this.space_000.setPosition(pos_5.x, pos_5.y - (this.speed - 85) * deltaTime, pos_5.z);

        const pos_6 = this.space_001.position.clone();
        if (pos_6.y < -1280) {
            pos_6.y = 1280;
        }
        this.space_001.setPosition(pos_6.x, pos_6.y - (this.speed - 85) * deltaTime, pos_6.z);


    }
}


